package com.example.pilipili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pilipili.common.R;
import com.example.pilipili.pojo.entity.Collection;
import com.example.pilipili.dao.CollectionMapper;
import com.example.pilipili.pojo.entity.Like;
import com.example.pilipili.service.CollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

    @Autowired
    CollectionMapper collectionMapper ;
    @Override
    public R<Collection> click(Collection entry) {
        QueryWrapper<Collection> qw = new QueryWrapper<>() ;
        qw.eq("uid",entry.getUid()).eq("vid",entry.getVid());

        if( !collectionMapper.exists(qw)) { // 点赞
            entry.setTime(LocalDate.now());
            collectionMapper.insert(entry) ;
            return R.ok(entry) ;
        }
        // 取消点赞
        collectionMapper.delete(qw) ;
        return R.ok();
    }

    @Override
    public boolean isCollection(int uid, int vid) {
        QueryWrapper<Collection> qw = new QueryWrapper<>() ;
        qw.eq("uid",uid).eq("vid",vid);
        return collectionMapper.exists(qw);
    }

}
